  <meta charset="utf-8" />
<?php

//使用$_POST数组获取表单中输入的修改后的数据
  $uid = $_POST['uid'];
  $uname = trim($_POST['uname']);  
  $password = $_POST['password'];
  $gender = $_POST['gender'];
  $birthdate = $_POST['birthdate'];
  $hobby = $_POST['hobby']; 
  $degree = $_POST['degree'];
  $ntro = $_POST['ntro'];
  $pic = $_POST['pic'];
//连数据库 
mysql_connect("localhost","root","1161105403");
mysql_select_db("neuvideo");
mysql_query("set names utf8");

//上传文件错误的判定
  if($_FILES["pic"]["error"]>0)
  {
    switch($_FILES["pic"]["error"])
    {
         case 1: echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值"; exit;
      case 3: echo "部分文件上传";  exit;
      case 4: echo "没有选择头像文件!"; 
        //如果没选择图片，则编写更新其他数据的sql语句
        $sql = "update users set uname='{$uname}',  password = '{$password}' ,birthdate='{$birthdate}', gender='{$gender}', hobby = '{$hobby}',degree = '{$degree}',ntro='{$ntro}' where uid={$uid}";  
        break;
      default: echo "未知错误"; exit; 
    }     
  }else {   //上传文件正常，更新数据表

    


//2.上传文件判断类型
	$allowtype=array("jpg","jpeg","png","gif","bmp","flv");
	$arr=explode(".",$_FILES["pic"]["name"]);
	$suffix=$arr[count($arr)-1];
	if(! in_array($suffix,$allowtype)){
		echo "文件类型错误！";
		exit;
	}else{
	echo"<br>照片已上传<br>";
	}
//3.拷贝文件
	$filepath="./images/";
	$randname=date("YmdHis").".".$suffix;
	move_uploaded_file($_FILES["pic"]["tmp_name"],$filepath.$randname);





      //获取原头像文件的文件名
      $sql="select * from users where uid={$uid}";
      $result=mysql_query($sql);
      $row=mysql_fetch_assoc($result);
      $filename=$filepath.$row["pic"];
      //删除原来的头像文件
      if(file_exists($filename))    
        unlink($filename);




 //编写SQL语句
  $sql = "update users set uname='{$uname}', password = '{$password}' ,birthdate='{$birthdate}', gender={$gender}, ntro='{$ntro}', pic='{$randname}' where uid={$uid}";      
  }


  //执行SQL语句
   $result = mysql_query($sql) or die("sql={$sql}, 更新失败！<br/>".mysql_error());
  //判断是否更新成功
  $num = mysql_affected_rows();//返回前一次 MySQL 操作所影响的记录行数
  if($num!=1){
    echo "更新失败！<br/>";
    echo "<a href='userList.php'>返回</a>";   
  }else{    
    echo "更新成功，3秒返回用户列表页";
		header("refresh:3;url='userList.php'");
  } 
 ?>	
